﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WF_QLThuVien.Utilities;
using WF_QLThuVien.BusinessLayer;
using WF_QLThuVien.Entities;

namespace WF_QLThuVien.PresentationLayer
{
    public partial class FormTimKiemDocGia : Form
    {
        #region: PANDA
        public CDocGiaBLL TKCDocGiaBLL;

        public delegate void UpdateData();
        public event UpdateData UpdateDate_Click;

        public delegate void AddressUpdateHandler(object sender, AddressUpdateEventArgs e);
        public event AddressUpdateHandler AddressUpdated;

        public delegate void CloseF(bool flag);
        public event CloseF dongForm;
        #endregion
        public FormTimKiemDocGia()
        {
            InitializeComponent();
            UpdateDate_Click += new UpdateData(FormTimKiemDocGia_Update_Click);
            dongForm += new CloseF(FormTimKiemDocGia_dongForm);
        }

        void FormTimKiemDocGia_dongForm(bool flag)
        {

        }

        void FormTimKiemDocGia_Update_Click()
        {

        }
        private void rdbTimChinhXac_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbTimChinhXac.Checked)
            {
                gbTimChinhXac.Enabled = true;
                gbTimGanDung.Enabled = false;
            }
            else
            {
                gbTimChinhXac.Enabled = false;
                gbTimGanDung.Enabled = true;
                chkTinhTrang_CheckedChanged(sender, e);
                chkNienKhoa_CheckedChanged(sender, e);
                chkNgaySinh_CheckedChanged(sender, e);
                chkNgayLapThe_CheckedChanged(sender, e);
                chkHoTen_CheckedChanged(sender, e);
                chkGioiTinh_CheckedChanged(sender, e);
            }
        }

        private void FormTimKiemDocGia_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;

            rdbTimChinhXac_CheckedChanged(sender, e);

            CPhatSinhMa.SoComboBox(CVariable.NIEN_KHOA_START, DateTime.Now.Year, cbbNKStart);
            cbbNKStart.SelectedIndex = 0;
            cbbNKStart.DropDownStyle = ComboBoxStyle.DropDownList;

            CPhatSinhMa.SoComboBox(CVariable.NIEN_KHOA_START, DateTime.Now.Year + CVariable.SO_NAM_HOC, cbbNKEnd);
            cbbNKEnd.SelectedIndex = CVariable.SO_NAM_HOC;
            cbbNKEnd.DropDownStyle = ComboBoxStyle.DropDownList;

            CPhatSinhMa.ChuoiComboBox(CVariable.TRANG_THAI_DOC_GIA, cbbTinhTrang);
            cbbTinhTrang.DropDownStyle = ComboBoxStyle.DropDownList;
            cbbTinhTrang.SelectedIndex = 0;
        }

        private void txtTimChinhXac_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtHoTen_TextChanged(object sender, EventArgs e)
        {

        }

        private void chkHoTen_CheckedChanged(object sender, EventArgs e)
        {
            if (chkHoTen.Checked)
            {
                txtHoTen.Enabled = true;
            }
            else
            {
                txtHoTen.Enabled = false;
            }
        }

        private void chkNgaySinh_CheckedChanged(object sender, EventArgs e)
        {
            if (chkNgaySinh.Checked)
            {
                dtpNgaySinh.Enabled = true;
            }
            else
            {
                dtpNgaySinh.Enabled = false;
            }
        }

        private void chkGioiTinh_CheckedChanged(object sender, EventArgs e)
        {
            if (chkGioiTinh.Checked)
            {
                rdbNam.Enabled = true;
                rdbNu.Enabled = true;
            }
            else
            {
                rdbNam.Enabled = false;
                rdbNu.Enabled = false;
            }
        }

        private void chkNgayLapThe_CheckedChanged(object sender, EventArgs e)
        {
            if (chkNgayLapThe.Checked)
            {
                dtpNgayLapThe.Enabled = true;
            }
            else
            {
                dtpNgayLapThe.Enabled = false;
            }
        }

        private void chkTinhTrang_CheckedChanged(object sender, EventArgs e)
        {
            if (chkTinhTrang.Checked)
            {
                cbbTinhTrang.Enabled = true;
            }
            else
            {
                cbbTinhTrang.Enabled = false;
            }
        }

        private void chkNienKhoa_CheckedChanged(object sender, EventArgs e)
        {
            if (chkNienKhoa.Checked)
            {
                cbbNKStart.Enabled = true;
                cbbNKEnd.Enabled = true;
            }
            else
            {
                cbbNKStart.Enabled = false;
                cbbNKEnd.Enabled = false;
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            try
            {
                if (rdbTimChinhXac.Checked == true)
                {
                    if (rdbMaDocGia.Checked == true)
                    {
                        string maDocGia = txtTimChinhXac.Text.ToUpper();
                        CDocGia dg = TKCDocGiaBLL.timMaDocGia(maDocGia);
                        if (dg.DocGiaID != "")
                        {
                            AddressUpdateEventArgs args = new AddressUpdateEventArgs(maDocGia, "", "");
                            AddressUpdated(this, args);
                            UpdateDate_Click();
                        }
                    }
                    else if (rdbMaSinhVien.Checked == true)
                    {
                        string maSinhVien = txtTimChinhXac.Text;
                        CDocGia dg = TKCDocGiaBLL.timMaSinhVien(maSinhVien);
                        if (dg.DocGiaID != "")
                        {
                            AddressUpdateEventArgs args = new AddressUpdateEventArgs("", maSinhVien, "");
                            AddressUpdated(this, args);
                            UpdateDate_Click();
                        }
                    }
                    else
                    {
                        string soDT = txtTimChinhXac.Text;
                        CDocGia dg = TKCDocGiaBLL.timSoDTSV(soDT);
                        if (dg.DocGiaID != "")
                        {
                            AddressUpdateEventArgs args = new AddressUpdateEventArgs("", "", soDT);
                            AddressUpdated(this, args);
                            UpdateDate_Click();
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {

            }
        }

        private void FormTimKiemDocGia_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {               
                this.Dispose();
            }
        }

        private void FormTimKiemDocGia_FormClosed(object sender, FormClosedEventArgs e)
        {
            dongForm(true);
        }

        
    }
}
